<template>
  <div>
    <el-dialog title="分配人员" v-if="dialogUserOpen" :close-on-click-modal="false" :visible.sync="dialogUserOpen" width="1000px" append-to-body>
        <!--用户数据-->
          <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
            <el-form-item label="用户名称" prop="userName">
              <el-input
                v-model="queryParams.userName"
                placeholder="请输入用户名称"
                clearable
                size="small"
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input
                v-model="queryParams.phonenumber"
                placeholder="请输入手机号码"
                clearable
                size="small"
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-select
                v-model="queryParams.status"
                placeholder="用户状态"
                clearable
                size="small"
                style="width: 240px"
              >
                <el-option
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="创建时间">
              <el-date-picker
                v-model="dateRange"
                size="small"
                style="width: 240px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

      <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50" align="center"/>
        <el-table-column label="用户编号" align="center" key="userId" prop="userId" />
        <el-table-column label="用户名称" align="center" key="userName" prop="userName"
                         :show-overflow-tooltip="true"/>
        <el-table-column label="用户昵称" align="center" key="nickName" prop="nickName"
                         :show-overflow-tooltip="true"/>
        <el-table-column label="部门" align="center" key="deptName" prop="dept.deptName"
                         :show-overflow-tooltip="true"/>
        <el-table-column label="手机号码" align="center" key="phonenumber" prop="phonenumber"
                         width="120"/>
        <el-table-column label="状态" align="center" key="status">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              disabled
              active-value="0"
              inactive-value="1"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="160">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="multiple" @click="saveUserSubmit">确 定</el-button>
        <el-button @click="dialogUserOpen=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listUser} from "@/api/system/user";

  export default {
    name: "dialog-user",
    //接受父组件传来的值
    props: {
      //弹出层
      dialogUserOpenProp: {
        type: Boolean,
        default(){
            return false;
          }
        },
      //默认多选  true为单选
      flag: {
        type: Boolean,
        default(){
          return false;
        }
      },
    },
    watch:{
      dialogUserOpenProp(){
        this.dialogUserOpen = this.dialogUserOpenProp;
      }
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        userArray: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        //弹出层
        dialogUserOpen:false,
        // 状态数据字典
        statusOptions: [],
        // 总条数
        total: 0,
        userList: [],
        // 日期范围
        dateRange: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userName: undefined,
          phonenumber: undefined,
          status: undefined,
          deptId: undefined
        },
      }
    },
    created() {
      this.getList();
      this.getDicts("sys_normal_disable").then(response => {
        this.statusOptions = response.data;
      });
    },
    methods: {
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.userArray = selection.map(item => item);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      saveUserSubmit(){
        if(this.flag==true){
          if(this.userArray.length==1){
            this.$emit("saveUserSubmit",this.userArray);
          }else{
            this.msgWarning("只能选择一个用户！");
          }
        }else{
          this.$emit("saveUserSubmit",this.userArray);
        }

      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 查询用户列表 */
      getList() {
        this.loading = true;
        listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.userList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
    }
  }
</script>

<style scoped>

</style>
